import request from '../request/request'
import axios from 'axios'
import { adbUrl } from '../indexUrl'
// 创建新的axios实例用于不同的API地址
const deviceScanApi = axios.create({
  baseURL: adbUrl,
  timeout: 10000,
  headers: {
    'Content-Type': 'application/json'
  }
})

// ADB设备列表接口
export const getAdbDeviceList = (serviceName: string) => {
  return request.get('/api/device/adbCommendDeviceList', {
    service_name: serviceName
  })
}

// 无ADB设备扫描信息接口
export const getServerInfo = () => {
  return deviceScanApi.get('/api/devices/server_info')
}

// 开始扫描无ADB设备
export const scanDevices = (params: {
  start_ip: string;
  end_ip: string;
  port: number;
}) => {
  return deviceScanApi.post('/api/devices/scan_connect', params)
}

// 获取扫描到的无ADB设备列表
export const getServerDevices = () => {
  return deviceScanApi.get('/api/devices/servers')
}

// 批量开启端口接口
export const batchOpenPort = (params: {
  service_name: string;
  device_list: string[];
}) => {
  return request.post('/api/device/batchOpenPort', params)
}

// 连接设备并入库接口
export const connectDeviceAndAddToDB = (serviceName: string) => {
  return request.get('/api/device/connectDeviceAndAddToDB', {
    service_name: serviceName
  })
}

// 无ADB设备入库接口
export const registerDevice = (serviceName: string) => {
  return request.get('/api/device/register', {
    service_name: serviceName
  })
}

// 获取设备列表接口
export interface DeviceQueryParams {
  page: number;
  page_size: number;
  service_name?: string;
  where?: {
    status?: boolean;
    android_version?: string;
    vip_level?: number;
    rent_status?: boolean;
    consul_id?: string;
    adb_type?: boolean;
    identification_number?: string;
  };
  sort?: string;
  order?: 'asc' | 'desc';
}

export const getDeviceList = (params: DeviceQueryParams) => {
  return request.post('/api/device/get', params)
}

// 节点信息接口
interface NodeInfo {
  service_id: string
  consul_addr: string
  consul_port: number
  service_name: string
  service_addr: string
  service_port: number
  network_usage: number
  cpu_usage: number
  memory_usage: number
  disk_usage: number
  connection_count: number
  user_count: number
  max_connections: number
  max_conn_time: string
  max_network_usage: number
  temperature: number
  status: string
}

// 获取节点列表
export const getNodeList = () => {
  return request.get<{
    code: number
    msg: string
    message: string
    data: Record<string, NodeInfo>
  }>('/api/node/get')
}

// 修改设备信息
export const modifyPhone = (data: {
  modify_id: number[]
  vip_level?: number
  status?: boolean
}) => {
  return request.post('/api/device/modifyPhone', data)
}

// 获取套餐列表
export interface VipPackage {
  id: number
  created_at: string
  updated_at: string
  vip_name: string
  level: number
}

export const getVipPackages = () => {
  return request.get<{
    code: number
    msg: string
    data: VipPackage[]
  }>('/api/package/vip')
}

// 获取VIP等级列表
export const getVipLevels = () => {
  return request.get('/api/vip/get')
}

// 获取套餐列表
export const getPackageList = (params: {
  vip_level: string;
  side: string;
}) => {
  return request.get('/api/package/list', params)
}

// 添加套餐
export interface AddPackageParams {
  system_version: number;
  vip_level: number;
  activate_package: Array<{
    day: number;
    tag: string;
    price: Record<string, number>;
    title: string;
    origin_price: number;
    activate_name?: string;
    renew_display: boolean;
    actually_package_id: number;
  }>;
}

export const addPackage = (data: AddPackageParams) => {
  return request.post('/api/package/add', data)
}

// 获取系统版本列表
export const getSystemVersions = () => {
  return request.get('/api/system/version/list')
}

// 获取安卓版本列表
export interface AndroidVersion {
  id: number
  created_at: string
  updated_at: string
  android_name: string
}

export interface AndroidVersionResponse {
  list: AndroidVersion[]
  page: number
  size: number
  total: number
}

export const getAndroidVersionList = (params: {
  page: number
  pageSize: number
}) => {
  return request.get<{
    code: number
    msg: string
    data: AndroidVersionResponse
  }>('/api/androidVersion/list', params)
}

// 更新套餐信息
export const updatePackage = (data: {
  system_version: number
  vip_level: number
  activate_package: Array<{
    day: number
    tag: string
    price: Record<string, number>
    title: string
    origin_price: number
    activate_name: string
    renew_display: boolean
    actually_package_id: number
  }>
}) => {
  return request.put('/api/package/update', data)
}

// 添加删除套餐接口
export const deletePackage = (packageId: number) => {
  return request.delete('/api/package/del', {
    Package_id: packageId
  })
}


// 删除系统版本 - 单个删除
export const deleteAndroidVersion = (id: number) => {
  return request.delete(`/api/androidVersion/${id}`)
}

// 批量删除系统版本
export const batchDeleteAndroidVersions = (ids: number[]) => {
  return request.post('/api/androidVersion/batchDelete', ids)
}

// 添加VIP等级
export const addVipLevel = (data: {
  vip_name: string
  level: number
}) => {
  return request.post('/api/vip/add', data)
}

// 更新VIP等级
export const updateVipLevel = (data: {
  id: number
  vip_name: string
}) => {
  return request.put('/api/vip/update', data)
}

// 删除VIP等级
export const deleteVipLevelById = (vipID: number) => {
  return request.delete('/api/vip/del', {
    vipID
  })
}

// 删除设备
export const deleteDevice = (deviceID: number) => {
  return request.delete('/api/device/del', {
    deviceID
  })
}

// 画面参数接口
export interface BitrateParams {
  id?: number
  name: string
  max_size: number
  video_bite_rate: number
  audio_bite_rate: number
  fps: number
}

// 获取画面参数列表
export const getBitrateList = () => {
  return request.get<{
    code: number
    msg: string
    message: string
    data: BitrateParams[]
  }>('/api/bitrate')
}

// 添加画面参数
export const addBitrate = (data: BitrateParams) => {
  return request.post('/api/bitrate', data)
}

// 更新画面参数
export const updateBitrate = (id: number, data: BitrateParams) => {
  return request.put(`/api/bitrate/${id}`, data)
}

// 删除画面参数
export const deleteBitrate = (id: number) => {
  return request.delete(`/api/bitrate/${id}`)
}

// 执行设备命令
export const execDeviceCommand = (phoneIdentify: string, consulIP: string, command: string) => {
  // 确保consulIP包含协议前缀
  let apiUrl = consulIP.startsWith('http://') || consulIP.startsWith('https://') 
      ? `${consulIP}/api/devices/exec_command`
      : `http://${consulIP}/api/devices/exec_command`;
  
  // 添加查询参数
  apiUrl += `?host=${encodeURIComponent(phoneIdentify)}&command=${encodeURIComponent(command)}`;
  
  return fetch(apiUrl, {
      method: 'get',
      headers: {
          'Content-Type': 'application/json'
      }
  }).then(response => response.json());
}

